<template>
  <div class="family-meal-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>我家老人餐饮记录</span>
        </div>
      </template>
      
      <div class="filter-container">
        <el-form :inline="true" :model="queryParams">
          <el-form-item label="日期范围">
            <el-date-picker
              v-model="queryParams.dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="餐次">
            <el-select v-model="queryParams.mealType" placeholder="请选择餐次" clearable>
              <el-option label="早餐" value="BREAKFAST" />
              <el-option label="午餐" value="LUNCH" />
              <el-option label="晚餐" value="DINNER" />
              <el-option label="加餐" value="SNACK" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="5" animated />
      </div>
      
      <div v-else>
        <el-table
          :data="mealRecords"
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="mealDate" label="日期" width="120">
            <template #default="{ row }">
              {{ formatDate(row.mealDate) }}
            </template>
          </el-table-column>
          <el-table-column prop="mealType" label="餐次" width="100">
            <template #default="{ row }">
              <el-tag :type="getMealTypeTag(row.mealType)">
                {{ getMealTypeText(row.mealType) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="mealContent" label="餐饮内容" min-width="200" />
          <el-table-column prop="intakeAmount" label="摄入量" width="100">
            <template #default="{ row }">
              {{ row.intakeAmount || '-' }}
            </template>
          </el-table-column>
          <el-table-column prop="specialDiet" label="特殊饮食" width="120">
            <template #default="{ row }">
              {{ row.specialDiet || '无' }}
            </template>
          </el-table-column>
          <el-table-column prop="recordedBy" label="记录人" width="120" />
          <el-table-column prop="recordTime" label="记录时间" width="180">
            <template #default="{ row }">
              {{ formatDateTime(row.recordTime) }}
            </template>
          </el-table-column>
        </el-table>
        
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="queryParams.pageNum"
            v-model:page-size="queryParams.pageSize"
            :total="total"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getFamilyMealRecords } from '@/api/family'

const loading = ref(false)
const tableLoading = ref(false)
const mealRecords = ref([])
const total = ref(0)

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  dateRange: [],
  mealType: ''
})

const getMealTypeTag = (mealType) => {
  const tagMap = {
    'BREAKFAST': 'primary',
    'LUNCH': 'success',
    'DINNER': 'warning',
    'SNACK': 'info'
  }
  return tagMap[mealType] || 'info'
}

const getMealTypeText = (mealType) => {
  const textMap = {
    'BREAKFAST': '早餐',
    'LUNCH': '午餐',
    'DINNER': '晚餐',
    'SNACK': '加餐'
  }
  return textMap[mealType] || mealType
}

const formatDate = (dateString) => {
  if (!dateString) return '-'
  return dateString.split('T')[0]
}

const formatDateTime = (dateTimeString) => {
  if (!dateTimeString) return '-'
  return dateTimeString.replace('T', ' ')
}

const loadMealRecords = async () => {
  tableLoading.value = true
  try {
    const params = {
      pageNum: queryParams.value.pageNum,
      pageSize: queryParams.value.pageSize,
      mealType: queryParams.value.mealType
    }
    
    if (queryParams.value.dateRange && queryParams.value.dateRange.length === 2) {
      params.startDate = queryParams.value.dateRange[0]
      params.endDate = queryParams.value.dateRange[1]
    }
    
    const response = await getFamilyMealRecords(params)
    if (response.code === 200) {
      mealRecords.value = response.data.records || []
      total.value = response.data.total || 0
    } else {
      ElMessage.warning(response.msg || '加载餐饮记录失败')
    }
  } catch (error) {
    console.error('加载餐饮记录失败:', error)
    ElMessage.error('加载餐饮记录失败')
  } finally {
    tableLoading.value = false
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  loadMealRecords()
}

const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    dateRange: [],
    mealType: ''
  }
  loadMealRecords()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  queryParams.value.pageNum = 1
  loadMealRecords()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  loadMealRecords()
}

onMounted(() => {
  loadMealRecords()
})
</script>

<style scoped>
.family-meal-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.filter-container {
  margin-bottom: 20px;
}

.loading-container {
  padding: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
